# Determine maximum value for p power of their digit number
# n digit number max value: n*9^p
#
# For p = 5
# 1 digit max value  59049 (>      9 so every number can be a solution)
# 2 digit max value 118098 (>     99 so every number can be a solution)
# 3 digit max value 177147 (>    999 so every number can be a solution)
# 4 digit max value 236196 (>   9999 so every number can be a solution)
# 5 digit max value 295245 (>  99999 so every number can be a solution)
# 6 digit max value 354294 (so every number until 354294 can be a solution)
# 7 digit max value 413343 (max value only have 6 digit, so no more solution)
#
# We need to test all number from 2 to 354294

def Solve():
    result = 0

    p = 5
    for i in xrange(2, 6*(9**p) + 1):
        x, s = i, 0
        while x != 0:
            x, d = divmod(x, 10)
            s += d**p
        if i == s:
            result += i

    return result


